# 函数案例ATM练习
import random
import sys

# 定义函数
def menu():
    """
    主菜单
    :return:
    """
    print("-----------------主菜单-----------------")
    print("您好，欢迎来到银行ATM,请选择要执行的操作")
    print("查询余额     [输入1]")
    print("存款        [输入2]")
    print("取款        [输入3]")
    print("推出        [输入4]")

def check_balance(balance):
    """
    查询余额
    :return:
    """
    print(f"您好，您的余额剩余:{balance}")

def save_money(balance):
    """
    存钱
    :param balance: 存钱之前的账户余额
    :return:
    """
    check_balance(balance)
    money = int(input("请输入存款金额："))
    balance = balance + money
    print(f"您好，您存款{money}元成功")
    print(f"您好，您的余额剩余{balance}元")
    return balance

def take_money(balance):
    """
    取钱
    :param balance: 取钱之前的账户余额
    :return:
    """
    check_balance(balance)
    money = int(input("请输入取款金额："))
    balance = balance - money
    print(f"您好，您取款{money}元成功")
    print(f"您好，您的余额剩余{balance}元")
    return balance

def next_step(balance):
    str_input = input("请输入是否继续操作，[y/n]")
    while True:
        if str_input == 'y' or str_input == 'Y':
            menu()
            choose = int(input("请输入要执行的操作的选项:"))
            if choose == 1:
                check_balance(balance)
                next_step(balance)
            elif choose == 2:
                save_balance = save_money(balance)
                next_step(save_balance)
            elif choose == 3:
                take_balance = take_money(balance)
                next_step(take_balance)
            elif choose == 4:
                print("请取回您的银行卡!!!")
                sys.exit()
            else:
                print("输入错误，请重新输入!!!")
                menu()
                choose = int(input("请输入您的选择:"))
        elif str_input == 'n' or str_input == 'N':
            print("请取回您的银行卡!!!")
            sys.exit()
        else:
            str_input = str(input("请重新输入是否继续操作，[y/n]"))


menu()
choose = int(input("请输入您的选择:"))
balance = random.randint(0, 10000000)       # 随机余额
flag = True
while flag:
    if choose == 1:
        check_balance(balance)
        next_step(balance)
    elif choose == 2:
        save_balance = save_money(balance)
        next_step(save_balance)
    elif choose == 3:
        take_balance = take_money(balance)
        next_step(take_balance)
    elif choose == 4:
        print("请取回您的银行卡!!!")
        sys.exit()
    else:
        print("输入错误，请重新输入!!!")
        menu()
        choose = int(input("请输入您的选择:"))